package com.srcbox.file.adapter

import android.app.Activity
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.os.Build
import android.view.LayoutInflater
import android.view.LayoutInflater.from
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.srcbox.file.R
import com.srcbox.file.ui.ShowApp
import kotlinx.android.synthetic.main.app_item.view.*
import java.util.*

class AppList(
    private val context: Activity,
    private val packageInfoList: ArrayList<PackageInfo?>?,
    private val hide: Boolean = false
) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == 0) {
            val view = LayoutInflater.from(context).inflate(R.layout.hide_item, null)
            println("隐藏2")
            HideItemViewHolder(view)
        } else {
            val inflate = from(parent.context).inflate(R.layout.app_item, parent, false)
            MyHolder(inflate, context)
        }
    }

    override fun getItemCount(): Int {
        return packageInfoList!!.size
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyHolder) {
            val packInfo = packageInfoList!![position]
            packInfo?.let { holder.bind(it) }
            holder.itemView.setOnClickListener {
                val intent = Intent(context, ShowApp::class.java)
                intent.putExtra("packageName", packInfo?.packageName)
                context.startActivity(intent)
            }
        }
        if (holder is HideItemViewHolder) {
            holder.bind()
        }
    }

    override fun getItemViewType(position: Int): Int {
        val packInfo = packageInfoList?.get(position)
        if (hide) {
            return if ((packInfo?.applicationInfo?.flags?.and(ApplicationInfo.FLAG_SYSTEM)) != 0) {
                0
            } else {
                1
            }
        }
        return 1
    }

    class MyHolder(itemView: View, private val context: Activity) :
        RecyclerView.ViewHolder(itemView) {
        @RequiresApi(Build.VERSION_CODES.O)
        fun bind(packageInfo: PackageInfo) {
            Thread {
                val appTitle = packageInfo.applicationInfo.loadLabel(context.packageManager)
                var drawable = packageInfo.applicationInfo.loadIcon(context.packageManager)

                context.runOnUiThread {
                    itemView.title.text = appTitle
                    itemView.icon.setImageDrawable(drawable)
                    drawable = null
                }
            }.start()
        }
    }

    class HideItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind() {
            itemView.layoutParams = ConstraintLayout.LayoutParams(0, 0)
            itemView.visibility = View.GONE
        }
    }
}


//        Data.isScroll = false

/*
fun setVisibility(visible: Boolean) {
    /*val param =
        itemView.layoutParams as RecyclerView.LayoutParams
    if (visible) {
        param.height = LinearLayout.LayoutParams.WRAP_CONTENT
        param.width = LinearLayout.LayoutParams.MATCH_PARENT
        itemView.visibility = View.VISIBLE
    } else {
        itemView.visibility = View.GONE
        param.height = 0
        param.width = 0
    }
    itemView.layoutParams = param*/
}
*/


/*   val rootLayout = LinearLayout(context)
   val rootLayoutParams = WindowManager.LayoutParams()
   rootLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT
   rootLayoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT
   rootLayout.gravity = Gravity.CENTER
   rootLayout.setPadding(10, 10, 10, 10)
   rootLayout.orientation = LinearLayout.VERTICAL
   rootLayout.layoutParams = rootLayoutParams

   val titleV = TextView(context)
   titleV.setTextColor(Color.parseColor(AppSetting.FONT_COLOR))
   val iconV = ImageView(context)
   rootLayout.addView(iconV)
   rootLayout.addView(titleV)
   parent.addView(rootLayout)
*/